#!/bin/bash

SUDO="sudo "
if [[ $(id -u) -eq 0 ]]; then SUDO=""; fi

read -p "Enter python3 version (default 3.10, e.g. 3.8 | 3.9 | 3.11 | 3.12 etc.): " version </dev/tty
version=$(echo "$version" | tr -s '[:space:]')
if [[ "$version" == "" ]]; then version="3.10"; fi

$SUDO mkdir -p /opt/python3/${version}

if [[ -x /opt/python3/$version/bin/python3 ]]; then
    read -p "python3[${version}] is installed, reinstall it? (y/N) " reinstall </dev/tty
    reinstall=$(echo "$reinstall" | tr -s '[:space:]')
    if [[ "$reinstall" != [Yy] ]]; then exit 0; fi
fi
$SUDO rm -rf /opt/python3/${version}/*

DOWN="https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64"


case $version in
    3.8)
        wget $DOWN/python-3.8.5-hcff3b4d_0.tar.bz2 -O python3.tar.bz2
        wget $DOWN/pip-24.0-py38h06a4308_0.tar.bz2 -O pip.tar.bz2
        wget $DOWN/openssl-1.1.1w-h7f8727e_0.tar.bz2 -O openssl.tar.bz2
        wget $DOWN/libffi-3.3-he6710b0_2.tar.bz2 -O libffi.tar.bz2
        ;;
    3.9)
        wget $DOWN/python-3.9.19-h955ad1f_1.tar.bz2 -O python3.tar.bz2
        wget $DOWN/pip-24.0-py39h06a4308_0.tar.bz2 -O pip.tar.bz2
        wget $DOWN/openssl-3.0.9-h7f8727e_0.tar.bz2 -O openssl.tar.bz2
        wget $DOWN/libffi-3.4.4-h6a678d5_1.tar.bz2 -O libffi.tar.bz2
        ;;
    3.10)
        wget $DOWN/python-3.10.14-h955ad1f_1.tar.bz2 -O python3.tar.bz2
        wget $DOWN/pip-24.0-py310h06a4308_0.tar.bz2 -O pip.tar.bz2
        wget $DOWN/openssl-3.0.9-h7f8727e_0.tar.bz2 -O openssl.tar.bz2
        wget $DOWN/libffi-3.4.4-h6a678d5_1.tar.bz2 -O libffi.tar.bz2
        ;;
    3.11)
        wget $DOWN/python-3.11.9-h955ad1f_0.tar.bz2 -O python3.tar.bz2
        wget $DOWN/pip-24.0-py311h06a4308_0.tar.bz2 -O pip.tar.bz2
        wget $DOWN/openssl-3.0.9-h7f8727e_0.tar.bz2 -O openssl.tar.bz2
        wget $DOWN/libffi-3.4.4-h6a678d5_1.tar.bz2 -O libffi.tar.bz2
        ;;
    3.12)
        wget $DOWN/python-3.12.2-h996f2a0_0.tar.bz2 -O python3.tar.bz2
        wget $DOWN/pip-24.0-py312h06a4308_0.tar.bz2 -O pip.tar.bz2
        wget $DOWN/openssl-3.0.9-h7f8727e_0.tar.bz2 -O openssl.tar.bz2
        wget $DOWN/libffi-3.4.4-h6a678d5_1.tar.bz2 -O libffi.tar.bz2
        ;;
    *)
        echo "error version"
        exit -1
esac

if [[ ! -f python3.tar.bz2 ]]; then echo "python3 is failured to download"; exit -1; fi
if [[ ! -f openssl.tar.bz2 ]]; then echo "openssl is failured to download"; exit -1; fi
if [[ ! -f pip.tar.bz2 ]]; then echo "pip is failured to download"; exit -1; fi
if [[ ! -f libffi.tar.bz2 ]]; then echo "libffi is failured to download"; exit -1; fi

$SUDO tar jxvf python3.tar.bz2 -C /opt/python3/${version}
$SUDO tar jxvf openssl.tar.bz2 -C /opt/python3/${version}
$SUDO tar jxvf pip.tar.bz2 -C /opt/python3/${version}
$SUDO tar jxvf libffi.tar.bz2 -C /opt/python3/${version}

rm -f python3.tar.bz2 openssl.tar.bz2 pip.tar.bz2 libffi.tar.bz2

mkdir -p ~/.pip && cat << EOF | tee ~/.pip/pip.conf
[global]
index-url = https://pypi.mirrors.ustc.edu.cn/simple/
[install]
trusted-host = pypi.mirrors.ustc.edu.cn
EOF

$SUDO sed '1c #!/opt/python3/'${version}'/bin/python3' -i /opt/python3/${version}/bin/pip
$SUDO sed '1c #!/opt/python3/'${version}'/bin/python3' -i /opt/python3/${version}/bin/pip3

$SUDO /opt/python3/${version}/bin/python3 -m pip install -i https://pypi.mirrors.ustc.edu.cn/simple/ --trusted-host pypi.mirrors.ustc.edu.cn certifi numpy pandas python-dateutil PyYAML urllib3 wheel

echo -e "\nMake sure that the following content exists in the \$HOME/.bashrc"
echo -e "\texport PYTHON_HOME=/opt/python3/${version}"
echo -e "\texport PATH=\$PYTHON_HOME/bin:\$PATH"
